#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <set>
#include <map> 

#define flr(x, l, r) for (int x = l; x <= r; ++x)
#define frl(x, r, l) for (int x = r; x >= l; --x)
#define fmp(x, t) for (int x = h[t]; ~x; x = ne[x])
#define LL long long
#define mt memset
#define my memcpy
#define szf sizeof
#define INF 0x3f3f3f3f
#define in(x) scanf("%d", &x)
#define out(x) printf("%d", x)
#define inll(x) scanf("%lld", &x)
#define outll(x) printf("%lld", x)
#define pn printf("\n")
#define con continue
#define bk break
#define vc vector
#define pb push_back
#define sz size
#define PII pair<int, int>
#define x first
#define y second
#define P_q priority_queue
#define ft front
#define pf push_front
#define popf pop_front
#define it insert
#define ct count

using namespace std;

const int N = 510, M = 10010;

int n, m;
struct Edge {
    int a, b, w;
    bool f;
    bool operator< (const Edge &W) const {
        return w < W.w;
    }
}edge[M];
int p[N];
int h[N], e[N * 2], ne[N * 2], w[N * 2], idx;
int dist1[N][N], dist2[N][N];

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

void dfs(int u, int fa, int maxd1, int maxd2, int d1[], int d2[]) {
    d1[u] = maxd1, d2[u] = maxd2;
    
    fmp(i, u) {
        int j = e[i];
        if (j != fa) {
            int dt1 = maxd1, dt2 = maxd2;
            if (w[i] > dt1) dt2 = dt1, dt1 = w[i];
            else if (w[i] < dt1 && w[i] > dt2) dt2 = w[i];
            dfs(j, u, dt1, dt2, d1, d2);
        }
    }
}

int main() {
	in(n), in(m);
	
	flr (i, 0, m - 1) {
	    int a, b, c;
	    in(a), in(b), in(c);
	    edge[i] = {a, b, c};
	}
	
	sort(edge, edge + m);
	flr (i, 1, n) p[i] = i;
	
	LL sum = 0;
	mt (h, -1, szf h);
	flr (i, 0, m - 1) {
	    int a = edge[i].a, b = edge[i].b, w = edge[i].w;
	    int pa = find(a), pb = find(b);
	    if (pa != pb) {
	        p[pa] = pb;
	        sum += w;
	        add(a, b, w), add(b, a, w);
	        edge[i].f = true;
	    }
	}
	
	flr (i, 1, n) dfs(i, -1, -1e9, -1e9, dist1[i], dist2[i]);
	
	LL res = 1e18;
	flr (i, 0, m - 1) {
	    if (!edge[i].f) {
	        int a = edge[i].a, b = edge[i].b, w = edge[i].w;
	        LL t;
	        if (w > dist1[a][b])
	            t = sum + w - dist1[a][b];
	        else if (w > dist2[a][b])
	            t = sum + w - dist2[a][b];
	        res = min(res, t);
	    }
	}
    
    outll(res), pn;
    
    return 0;
}